package com.oasistudio.tk.connector.imp;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TreeMap;

import org.json.JSONObject;

import com.oasistudio.tk.connector.TopAPIConnectorHelper;
import com.oasistudio.tk.connector.TopApiRequest;
import com.oasistudio.tk.connector.TopApiResponse;
import com.taobao.tbw.common.TBWContext;


public class ItemGetConnector implements TopAPIConnectorHelper {
	private String itemId;
	private String fields="detail_url,num_iid,title,nick,type,cid,seller_cids,props,input_pids,input_str,desc,pic_url,num,valid_thru,list_time,delist_time,stuff_status,location,price,post_fee,express_fee,ems_fee,has_discount,freight_payer,has_invoice,has_warranty,has_showcase,modified,increment,approve_status,postage_id,product_id,auction_point,property_alias,item_img,prop_img,sku,video,outer_id,is_virtual";//�??????��????

	public void setItemId(String itemId){
		this.itemId = itemId;
	}

	public void setFields(String fields){
		this.fields = fields;
	}

	public ItemGetConnector() {
	}

	@Override
	public Object asyncPaser(byte[] part) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getApiUrl() {
		TopApiRequest request = new TopApiRequest();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		TreeMap<String, String> params = new TreeMap<String, String>();
		//???系�????
		params.put("method", "taobao.item.get");
		//params.put("session", TBWContext.getInstance().getTopsession());
		params.put("timestamp", sdf.format(calendar.getTime()));
		params.put("format", "json");
		params.put("app_key", TBWContext.getInstance().getAppKey());
		params.put("v", "2.0");
		params.put("partner_id", "top-apitools");
		params.put("sign_method", "md5");
		//API method ???
		params.put("num_iid", itemId);
		params.put("fields", fields);
		request.params = params;
		return request.generalRequestUrl(true);
	}

	/**
	 * �??�?????�???��? total_results Number ??��?��?交�?信�??��? trades Trade[] 交�?信�???��
	 * hash_next Boolean ??��???�?��
	 */
	@Override
	public Object syncPaser(byte[] all) {
		//System.out.println("mellon:ItemGetConnector.syncPaser() all = "+all);
		try {
			TopApiResponse response = new TopApiResponse("item_get_response");
			String str = new String(all, "UTF-8");
			//Log.d("return msg:", str);
			System.out.println("mellon:ItemGetConnector.syncPaser() str = "+str);
			//System.out.println("mellon:ItemGetConnector.syncPaser() response.parseResult(str).success = "+response.parseResult(str).success);
			if (response.parseResult(str).success) {
				JSONObject jsObj = response.data;
				//System.out.println("mellon:ItemGetConnector.syncPaser() jsObj.has(item) = "+jsObj.has("item"));
				if (jsObj.has("item")) {
					JSONObject itemJSON = jsObj.getJSONObject("item");
					Item item = new Item();
					item.num_iid = itemJSON.optLong("num_iid");
					item.num = itemJSON.optLong("num");
					item.title = itemJSON.optString("title");
					item.detail_url = itemJSON.optString("detail_url");
					item.pic_url = itemJSON.optString("pic_url");
					item.price = itemJSON.optString("price");
					item.nick = itemJSON.optString("nick");
					JSONObject locationJSON = itemJSON.optJSONObject("location");
					if(locationJSON!=null){
						item.location = new Location();
						item.location.city = locationJSON.optString("city");
						item.location.state = locationJSON.optString("state");
					}
					//item.location = itemJSON.optString("location");
					return item;
				}
			}else{
				System.out.println("mellon:ItemGetConnector.syncPaser() response.parseResult(str).success = "+response.parseResult(str).success);
				System.out.println("mellon:ItemGetConnector.syncPaser() apiurl = "+this.getApiUrl());
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static class Item {
		public String detail_url;//???�??页�??��?
		public Number num_iid;//????��?ID
		public String title;//?????��
		public String pic_url;//???主�???		
		public Number num;//????��?
		public Location location;//????????		
		public String price;//???价�?�??�??5.00�??�?????精确?��???		
		public String post_fee;//平�?
		public String express_fee;//�??
		public String ems_fee;//EMS
		public String freight_payer;//�?��?��??��?,seller�??家�????�?uyer(买�??��?�?		
		public String nick;//????�称
		
		public String toString(){
			StringBuilder sb = new StringBuilder();
			sb.append("num_iid:"+num_iid);
			sb.append(";title:"+title);
			sb.append(";num:"+num);
			sb.append(";price:"+price);
			return sb.toString();
		}
	}
	
	public static class Location{
		public String state;
		public String city;
		public String toString(){
			StringBuilder sb = new StringBuilder();
			sb.append(state+" "+city);
			return sb.toString();
		}		
	}
}
